import { getLunBo, getMessageList, getOrderList } from '../api/index';
import { formatTimeDifference, formatDeadline } from '../utils/commom';
import { store } from '../../store/index';

Page({
  data: {
    list: [{
      pagePath: "/pages/index/index",
      text: "首页",
      iconPath: "/icons/tab-bar/index.png",
      selectedIconPath: "/icons/tab-bar/index-selected.png"
    }],
    // 居中显示项的位置
    centerItem: 0,
    // 消息列表
    messageStr: '',
    // 首页轮播图数据
    coverList: [
    ],
    orderList: [],
    pageData: {
    },
    total: 0
  },

  handleSwiperChange(e) {
    this.setData({
      centerItem: e.detail.current,
    })
  },

  onShow() {

  this.getOrderList();

  },
  onLoad() {
    wx.getStorageSync('openId') && store.getUserInfo();
    this.lunboData();
  },

  lunboData() {
    getLunBo().then(res => {
      console.log(res, 'res');
      this.setData({
        coverList: res.result
      });
    });
    getMessageList().then(res => {
      let messageStr = '';
      res.result.forEach(item => {
        messageStr += item.content + ' ';
      });
      this.setData({
        messageStr
      });
    });
  },
  getOrderList() {
    // wx.showLoading({
    //   title: '加载中',
    // });
    getOrderList(this.data.pageData).then(res => {
      
      this.setData({
        orderList: res.result?.map(item => ({
          ...item,
          createTime: formatTimeDifference(item.createTime),
          lastTime: formatDeadline(item.lastTime)
        })),
        // total: res.result.total
      });
    }).finally(() => {
      // wx.hideLoading();
    });
  },

  change_to_form(e) {
    wx.navigateTo({
      url: `/pages/selectOrder/index?type=${e.currentTarget.dataset.name}`,
    })
  }
})